__author__ = 'shawhen'

import subprocess
import socket

from ..process import uprocess as m_uprocess
from ..platform import what as m_what_platform
from . import netstat as m_netstat


def is_idle(addr, protocol, port_num):
    """
    判断某个地址上的指定协议的端口是否可用
    :return True/False
    """
    protocol_map = {
        "udp": socket.SOCK_DGRAM,
        "tcp": socket.SOCK_STREAM,
    }
    sock = socket.socket(type=protocol_map[protocol])
    try:
        sock.bind((addr, port_num))
        sock.close()
        return True
    except OSError as e:
        if "Win" in str(e) and "10048" in str(e):  # windows 端口被占用错误码
            return False
        else:
            import traceback
            traceback.print_exc()

            return False
    except Exception as e:
        import traceback
        traceback.print_exc()

        return False